//
//  GDWebAdvertiseViewController.m
//  Gunterapple
//
//  Created by 热带雨林 on 2021/1/1.
//  Copyright © 2021 热带雨林. All rights reserved.
//

#import "GDWebAdvertiseViewController.h"
#import <WebKit/WebKit.h>


@interface GDWebAdvertiseViewController ()<WKNavigationDelegate>
//加载的网页
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) UIView *navigationView;
@property (nonatomic, strong) UIProgressView *progressView;//进度条

@end

@implementation GDWebAdvertiseViewController

//预防webview白屏问题
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //webView.title = nil,就白屏了
    if(self.webView.title == nil){
        [self.webView reload];
    }
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //视图将要退出时，移除进度条
    [self.progressView removeFromSuperview];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //注意：此控制器不是一个navigationController 是modal出来的，只能自定义navigationbar
    self.view.backgroundColor = [UIColor redColor];
    //广告页思路一需要添加导航条，思路二不需要
//    [self setUpNavigationView];
    [self setUpWebView];
    
}
#pragma mark - 导航栏
- (void)setUpNavigationView{
    self.navigationView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, GDScreenW,SJHeight)];
    self.navigationView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.navigationView];
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(5, 40, 69, 44)];
    [backBtn setTitle:@"关闭" forState:UIControlStateNormal];
//    backBtn.titleLabel.textColor = [UIColor blackColor];
//    [backBtn.titleLabel setTextColor:[UIColor blackColor]];
    //在设置时制定了模式，在设置颜色时页需要指定颜色
    [backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(setMainVC) forControlEvents:UIControlEventTouchUpInside];
//    backBtn.backgroundColor = [UIColor redColor];
    backBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    [self.navigationView addSubview:backBtn];
    self.progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, SJHeight - 3, GDScreenW, 2)];
    self.progressView.progressViewStyle = UIProgressViewStyleBar;
    self.progressView.progressTintColor = [UIColor blueColor];
    [self.navigationView addSubview:self.progressView];
}
- (void)setUpWebView{
    
    self.webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, SJHeight, GDScreenW, GDScreenH - SJHeight)];
    self.webView.backgroundColor = [UIColor whiteColor];
    //通过代理解决白屏问题
    self.webView.navigationDelegate = self;
    [self.view addSubview:self.webView];
    
    //KVO检测webView的加载进度
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
    
    if(!self.urlStr) return;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr]];
    [self.webView loadRequest:request];
    
    //进度条
    self.progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, SJHeight - 44, GDScreenW, 2)];
    self.progressView.progressViewStyle = UIProgressViewStyleBar;
    self.progressView.progressTintColor = [UIColor blueColor];
    [self.navigationController.navigationBar addSubview:self.progressView];

}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if([keyPath isEqualToString:@"estimatedProgress"]){
        //将进度转成数据
        CGFloat progress =  [change [NSKeyValueChangeNewKey] floatValue];
        //
        [self.progressView setProgress:progress animated:YES];
        if(progress == 1.0){
            //延时
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                //网页加载完了，置空进度条
                [self.progressView setProgress:0.0 animated:NO];
            });
        }
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
#pragma mark - 通过代理解决白屏问题
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView{
    [webView reload];
}

/**
 通过代理的方式跳转回根控制器，但是会先返回广告页
 */
-(void)clickBack{
   //modal结束之后通过代理
    [self dismissViewControllerAnimated:NO completion:^{
        if([self.delegate respondsToSelector:@selector(bakcNa)]){
            [self.delegate bakcNa];
        }
    }];
}

- (void)setMainVC{
    //广告页思路一
    //通过传值指定根控制器
    self.view.window.rootViewController = self.mainNaVC;
    //网页返回
    if([self.webView canGoBack])
        [self.webView goBack];
}

#pragma mark - dealloc
- (void)dealloc{
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
